package com.example.teachdemo05;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.teachdemo05.fragments.FxFragment;
import com.example.teachdemo05.fragments.TxlFragment;
import com.example.teachdemo05.fragments.WdFragment;
import com.example.teachdemo05.fragments.WxFragment;

public class FragmentDemoActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    WxFragment wxFragment = null;
    TxlFragment txlFragment = null;
    FxFragment fxFragment = null;
    WdFragment wdFragment = null;
    RadioGroup rg_tabbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_demo);

        rg_tabbar = findViewById(R.id.rg_tabbar);

        rg_tabbar.setOnCheckedChangeListener(this);
        if(wxFragment == null){
            wxFragment = new WxFragment();
        }


//        FragmentManager manager = getSupportFragmentManager();
//        FragmentTransaction transaction = manager.beginTransaction();
//        transaction.replace(R.id.fl_content,wxFragment);
//        transaction.commit();

        getSupportFragmentManager().beginTransaction().replace(R.id.fl_content,wxFragment).commit();

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if(id == R.id.rb_wx){
            if(wxFragment == null){
                wxFragment = new WxFragment();
            }
            transaction.replace(R.id.fl_content,wxFragment);
        }
        if(id == R.id.rb_txl){
            if(txlFragment == null){
                txlFragment = new TxlFragment();
            }
            transaction.replace(R.id.fl_content,txlFragment);

        }
        if(id == R.id.rb_fx){
            if(fxFragment == null){
                fxFragment = new FxFragment();
            }
            transaction.replace(R.id.fl_content,fxFragment);

        }
        if(id == R.id.rb_wd){
            if(wdFragment == null){
                wdFragment = new WdFragment();
            }
            transaction.replace(R.id.fl_content,wdFragment);

        }
        transaction.commit();
    }
}
